<?php

namespace Common\Model;

use Think\Model;

class AnswerPraiseModel extends Model
{
    /**
     * 添加点赞
     * @param string $aid
     * @param string $uid
     * @return bool|string
     */
    public function inc_praise($aid = '', $uid = '')
    {
        $map['aid'] = intval($aid);
        $map['uid'] = intval($uid);
        $praise = $this->where($map)->find();
        if (empty($praise)) {
            if ($this->create($map)) {
                $this->add();
                return $aid;
            }
        }
        return false;
    }

    public function praise_add($uid, $aid)
    {
        $data = compact('uid', 'aid');
        if (!$this->create($data)) {
            $res['status'] = false;
            $res['msg'] = $this->getError();
            return $res;
        } else {
            $newid = $this->add();
            if (!$newid) {
                $res['status'] = false;
                $res['msg'] = '点赞失败';
                return $res;
            } else {
                # 点赞成功相关操作
                D('Answer')->inc_praise($aid);//修改回复点赞数字段
                $praise = D('Answer')->get_praise($aid);//获取回复点赞数量
                //更新会员点赞总数
                $answer = M('Answer')->find($aid);
                D('User')->inc_praise($answer['uid']);
                D('UserTrack')->add_track($uid, $answer['qid'], 4, $aid);//写入个人动态
                D('Rank')->add_record('praise', $answer['uid'], 1);  // 记录排行数据

                $res['status'] = true;
                $res['msg'] = '点赞成功';
                return $res;
            }
        }
    }
}